package com.v2future.sjt.collegecheckingattendance.rollcall.adapter;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

import com.v2future.sjt.collegecheckingattendance.R;
import com.v2future.sjt.collegecheckingattendance.rollcall.bean.StudentBean;
import com.v2future.sjt.collegecheckingattendance.rollcall.bean.SubjectBean;

import java.util.List;

/**
 * Created by minnie on 16/2/16.
 */
public class StudentsListAdapter extends BaseAdapter {

    List<StudentBean> dataList;
    Context mContext;

    public StudentsListAdapter(List<StudentBean> dataList, Context mContext) {
        this.dataList = dataList;
        this.mContext = mContext;
    }

    @Override
    public int getCount() {
        return dataList.size();
    }

    @Override
    public Object getItem(int position) {
        return dataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        final StudentBean bean = dataList.get(position);
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(
                    R.layout.listitem_student_absent, null);
            holder = new ViewHolder();
            holder.nameTv = (TextView) convertView.findViewById(R.id.tv_name);
            holder.absentStatusTv = (TextView) convertView.findViewById(R.id.tv_absent);
            holder.lateStatusTv = (TextView) convertView.findViewById(R.id.tv_late);
            holder.leaveStatusTv = (TextView) convertView.findViewById(R.id.tv_leave);
            holder.checkTv = (TextView) convertView.findViewById(R.id.tv_check);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.nameTv.setText(bean.getUserName());
        holder.absentStatusTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!bean.getLeaveType().equals("3")) {
                    bean.setLeaveType("3");
                }
                else {
                    bean.setLeaveType("0");
                }
                notifyDataSetChanged();
            }
        });
        holder.lateStatusTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!bean.getLeaveType().equals("4")) {
                    bean.setLeaveType("4");
                }
                else {
                    bean.setLeaveType("0");
                }
                notifyDataSetChanged();
            }
        });
        holder.leaveStatusTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!bean.getLeaveType().equals("1")) {
                    bean.setLeaveType("1");
                }
                else {
                    bean.setLeaveType("0");
                }
                notifyDataSetChanged();
            }
        });
        String checkStatus = bean.getLeaveType();
        if ("0".equals(checkStatus)) {//正常
            setTextChecked(holder.lateStatusTv, false);
            setTextChecked(holder.absentStatusTv, false);
            setTextChecked(holder.leaveStatusTv, false);
        }
        else if("3".equals(checkStatus)) {
            setTextChecked(holder.lateStatusTv, false);
            setTextChecked(holder.absentStatusTv, true);
            setTextChecked(holder.leaveStatusTv, false);
        }
        else if ("4".equals(checkStatus)) {
            setTextChecked(holder.lateStatusTv, true);
            setTextChecked(holder.absentStatusTv, false);
            setTextChecked(holder.leaveStatusTv, false);
        }
        else if ("1".equals(checkStatus)) {
            setTextChecked(holder.lateStatusTv, false);
            setTextChecked(holder.absentStatusTv, false);
            setTextChecked(holder.leaveStatusTv, true);
        }
        else {
            setTextChecked(holder.lateStatusTv, false);
            setTextChecked(holder.absentStatusTv, false);
            setTextChecked(holder.leaveStatusTv, false);
        }
        return convertView;
    }

    public void setTextChecked(TextView tv, boolean checked){
        if (checked) {
            tv.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.sharp_btn_solid_red));
            tv.setTextColor(mContext.getResources().getColor(R.color.main_fore));
        }
        else {
            tv.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.sharp_btn_empty_gray));
            tv.setTextColor(mContext.getResources().getColor(R.color.main_bg));
        }
    }

    public class ViewHolder{
        TextView nameTv;
        TextView absentStatusTv;
        TextView lateStatusTv;
        TextView leaveStatusTv;
        TextView checkTv;
    }
}
